package cn.wolfcode.rbac.service.system_manage.impl;


import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.mapper.system_manage.DepartmentMapper;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.system_manage.IDepartmentService;
import cn.wolfcode.rbac.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @version 1.0
 * @Author:Cao Zhijian
 * @DATE:2020/12/24 0024 14:40:32
 */

@Service
public class IDepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Department get(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject queryObject) {

        int count = departmentMapper.selectForCount();

        if(count == 0){
            // 1.如果数据库中没有数据是不需要分页显示，直接返回一个空数据
            return new PageResult(1,3,Collections.emptyList(),0);
        }else {
            // 2.如果数据库中有数据就要分页显示
            List<Department> departments = departmentMapper.selectForList(queryObject);
            return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),departments,count);
        }
    }

    @Override
    public void saveOrUpdate(Department department) {
        if(department.getId() == null){
            departmentMapper.insert(department);
        }else {
            departmentMapper.updateByPrimaryKey(department);
        }
    }
}
